package com.huike.clues.service.impl;


import com.huike.clues.domain.TbClue;
import com.huike.clues.domain.TbClueTrackRecord;
import com.huike.clues.domain.vo.ClueTrackRecordVo;
import com.huike.clues.mapper.TbClueMapper;
import com.huike.clues.mapper.TbClueTrackRecordMapper;
import com.huike.clues.service.ITbClueService;
import com.huike.clues.service.ITbClueTrackRecordService;
import com.huike.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 线索跟进记录Service业务层处理
 * @date 2022-04-22
 */
@Service
public class TbClueTrackRecordServiceImpl implements ITbClueTrackRecordService {

    @Autowired
    private TbClueTrackRecordMapper tbClueTrackRecordMapper;

    @Autowired
    private TbClueMapper tbClueMapper;

    /**
     * 线索跟进
     *
     * @param tbClueTrackRecord
     */
    @Override
    @Transactional
    public void add(ClueTrackRecordVo tbClueTrackRecord) {
        TbClue tbClue = new TbClue();
        tbClue.setName(tbClueTrackRecord.getName());
        tbClue.setSex(tbClueTrackRecord.getSex());
        tbClue.setId(tbClueTrackRecord.getClueId());
        tbClue.setSubject(tbClueTrackRecord.getSubject());
        tbClue.setLevel(tbClueTrackRecord.getLevel());
        tbClue.setWeixin(tbClueTrackRecord.getWeixin());
        tbClue.setQq(tbClueTrackRecord.getQq());
        tbClue.setAge(tbClueTrackRecord.getAge());
        tbClue.setNextTime(tbClueTrackRecord.getNextTime());
        tbClue.setStatus(TbClue.StatusType.FOLLOWING.getValue());
        tbClue.setUpdateTime(new Date());
        tbClueMapper.updateTbClue(tbClue);

        TbClueTrackRecord record = new TbClueTrackRecord();
        record.setClueId(tbClueTrackRecord.getClueId());
        record.setSubject(tbClueTrackRecord.getSubject());
        record.setRecord(tbClueTrackRecord.getRecord());
        record.setLevel(tbClueTrackRecord.getLevel());
        record.setType(tbClueTrackRecord.getType());
        record.setFalseReason(tbClueTrackRecord.getFalseReason());
        record.setNextTime(tbClueTrackRecord.getNextTime()); // 下次跟进时间
        record.setCreateBy(SecurityUtils.getUsername());
        record.setCreateTime(new Date());
        tbClueTrackRecordMapper.insert(record);
    }

    @Override
    public List<TbClueTrackRecord> getList(Long clueId) {
        return tbClueTrackRecordMapper.getList(clueId);
    }

}
